package com.cskaoyan.service.wxauthanduser;


import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.vo.wxauthanduser.AuthRegisterInfo;
import com.cskaoyan.bean.vo.wxauthanduser.AuthRegisterVo;
import com.cskaoyan.bean.po.ohter.MarketUser;
import com.cskaoyan.bean.po.ohter.MarketUserExample;
import com.cskaoyan.bean.vo.wxauthanduser.UserInfo;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.common.Constant;
import com.cskaoyan.util.crypt.CryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/18 16:17
 * @Version 1.5
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MemoryBean memoryBean;

    /**
     * @Description : 微信小程序界面的用户登录
     * @Param :
     * @Return :
     */
    @Override
    public UserInfo vxAuthLogin(String username, String password, String ip) {
        UserInfo userInfo = new UserInfo();
        password = CryptUtils.encryptStr(password);
        MarketUser marketUser = marketUserMapper.selectUserNickNameAndAvatarUrlByUserNameAndPassword(username, password);
        if (marketUser == null) {
            userInfo = null;
            return userInfo;
        }

        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        marketUser.setLastLoginTime(new Date(System.currentTimeMillis()));//用户的最近一次登录时间更改
        marketUser.setLastLoginIp(ip);//用户最近一次登录IP更新
        marketUserMapper.updateByExample(marketUser, marketUserExample);

        userInfo.setNickName(marketUser.getNickname());
        userInfo.setAvatarUrl(marketUser.getAvatar());
        userInfo.setId(marketUser.getId());
        userInfo.setIp(null);
        return userInfo;
    }

    @Override
    public AuthRegisterVo wxAuthRegister(Map map) {
        MarketUserExample example1 = new MarketUserExample();
        MarketUserExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUsernameEqualTo((String) map.get("username"));
        List<MarketUser> users1 = marketUserMapper.selectByExample(example1);
        if (users1.size() > 0) {
            return new AuthRegisterVo(new AuthRegisterInfo(null, "xxx"), "xxx");
        }

        MarketUserExample example2 = new MarketUserExample();
        MarketUserExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andMobileEqualTo((String) map.get("mobile"));
        List<MarketUser> users2 = marketUserMapper.selectByExample(example2);
        if (users2.size() > 0) {
            return new AuthRegisterVo(new AuthRegisterInfo("xxx", null), "xxx");
        }

        MarketUser user = new MarketUser();
        user.setUsername((String) map.get("username"));
        String newPassword = CryptUtils.encryptStr((String) map.get("password"));
        user.setPassword(newPassword);
        user.setNickname((String) map.get("username"));
        user.setMobile((String) map.get("mobile"));
        user.setAvatar(Constant.DEFAULT_USER_AVATAR_URL);
        user.setAddTime(new Date(System.currentTimeMillis()));
        user.setUpdateTime(new Date(System.currentTimeMillis()));
        marketUserMapper.insertSelective(user);

        String username = (String) map.get("username");
        String password = (String) map.get("password");

        AuthRegisterInfo authRegisterInfo = new AuthRegisterInfo();
        AuthRegisterVo authRegisterVo = new AuthRegisterVo();

        authRegisterInfo.setNickName(username);
        authRegisterInfo.setAvatarUrl(Constant.DEFAULT_USER_AVATAR_URL);
        authRegisterVo.setUserInfo(authRegisterInfo);
        String token = CryptUtils.encryptStr(username + password + "ss");
        authRegisterVo.setToken(token);

        assert token != null;
        memoryBean.tokenAndUserId.put(token, user.getId());

        return authRegisterVo;
    }

    @Override
    public void wxAuthReset(String mobile, String password) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        MarketUser marketUser = new MarketUser();
        marketUser.setPassword(password);
        marketUserMapper.updateByExampleSelective(marketUser, example);
    }


}

